package com.leet.code.lcof;

public class Code30 {


    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(1);
        minStack.push(-12);
        minStack.push(12);
        minStack.push(-2);

        System.out.println(minStack.top());
        System.out.println(minStack.min());

    }

    public static class MinStack {

        private MinLinkNode top;

        public MinStack() {

        }

        public void push(int x) {
            if (top == null) {
                top = new MinLinkNode(x, x);
                return;
            }
            int min = x < top.min ? x : top.min;
            top.next = new MinLinkNode(x, min);
            top.next.prev = top;
            top = top.next;
        }

        public void pop() {
            if (top == null) {
                return;
            }
            top = top.prev;

        }

        public int top() {
            return top.val;
        }

        public int min() {
            return top.min;
        }


        public static class MinLinkNode {
            public int val;
            public int min;
            public MinLinkNode prev;
            public MinLinkNode next;

            public MinLinkNode(int val, int min) {
                this.min = min;
                this.val = val;
            }
        }

    }


}
